package com.gaohe.common;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.Arrays;

//AOP
@RestControllerAdvice
public class MyExceptionHandler {
    //1.定义拦截的异常类型
    //2.对应处理
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public Result nameUniqueException(Exception ex){
//        System.err.println(ex.getMessage());
        // Java 名称重复

        System.out.println("------------------");
        String message = ex.getCause().getMessage();
        if (message.contains("sys_tag.name")){
            String keyWord = message.split(" ")[2];
            return Result.error("标签名称："+keyWord+" 已存在，请重新输入");
        }
        return Result.error("非法操作");
    }
}
